//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================
// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy
//===============================================================================

using System;
using System.IO;

namespace EntLibContrib.Logging.Parser
{
	/// <summary>
	/// Capability not available in <see cref="System.IO.File"/>.
	/// </summary>
	public static class FileIO
	{
		#region Pubic methods
		/// <summary>
		/// Opens a text file, reads all lines of the file, and then closes the file.
		/// </summary>
		/// <param name="path">The file to open for reading.</param>
		/// <param name="share">Specifies the type of access others can have to the file.</param>
		/// <returns>
		/// A string array containing all lines of the file.
		/// </returns>
		/// <exception cref="OutOfMemoryException">Thrown with the path as the message when the file is too large to open.</exception>
		public static string ReadAllText(string path, FileShare share)
		{
			using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, share))
			{
				System.IO.StreamReader sr = new StreamReader(fs);
				try
				{
					return sr.ReadToEnd();
				}
				catch (OutOfMemoryException exc)
				{
					throw new LoggingFileSizeException(path, exc);
				}
			}
		}

		/// <summary>
		/// Read all the text from a file that is already open due to write.
		/// </summary>
		/// <param name="path">The file to open for reading.</param>
		/// <returns>
		/// A string array containing all lines of the file.
		/// </returns>
		/// <remarks>
		/// Avoids the <see cref="System.IO.IOException"/> :
		/// The process cannot access the file 'path'
		/// because it is being used by another process.
		/// </remarks>
		/// <exception cref="OutOfMemoryException">Thrown when the file too large to open.</exception>
		public static string ReadAllText(string path)
		{
			return ReadAllText(path, FileShare.ReadWrite);
		}
		#endregion
	}
}
